package com.laboratory.serviceprovide.util;

import com.Laboratory.utilPojo.emailMessage;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

/**
 * @Author acok
 * @description: 邮件工具
 * @Date 2023/1/5 9:48
 * @Version 1.0
 */
public class emailUtil {
  /**
       * @return: void
       * @param: 邮件实体类
       * @description: 通过前端传递的邮件实例，发送邮件
       * @Author: Acok
       * @mbggenerated 2023/4/25
       */
    public static void sendEmail(emailMessage email) throws MessagingException,AddressException {
        Properties prop = new Properties();
        prop.setProperty("mail.transport.protocol", "smtp");//定义邮件发送协议
        prop.setProperty("mail.smtp.host", "smtp.qq.com");//声明邮件服务器地址
        prop.setProperty("mail.smtp.auth", "true");//发送权限，为true时表示允许发送
        prop.setProperty("mail.debug", "true");//设置为true时，调试的时候可以在控制台显示信息


        Session session = Session.getInstance(prop);//相当于建立了一条通信路线
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(email.getFromAccount()));//发件者邮箱
        msg.setRecipient(RecipientType.TO, new InternetAddress(email.getAddressAccount()+"@qq.com"));//收件邮箱
        msg.setSubject(email.getEmailTitle());
        msg.setText(email.getEmailContent());

        Transport tran = session.getTransport();
        tran.connect(email.getFromAccount(), email.getFromPassword());//假设q号为982690136的密码为123456
        tran.sendMessage(msg, msg.getAllRecipients());
    }
}
